package it.canraid.leaflet.control.activity;

import it.canraid.control.IHandlerCommunication;
import it.canraid.leaflet.R;
import it.canraid.leaflet.control.ClickThumbnailListener;
import it.canraid.leaflet.control.ProviderGpsListener;
import it.canraid.leaflet.model.LinePill;
import it.canraid.leaflet.model.ReadMessagePill;
import it.canraid.leaflet.model.StatusPill;
import it.canraid.leaflet.model.WordPill;
import it.canraid.leaflet.view.ReadMessageView;
import it.canraid.leaflet.view.StatusMessageView;

import java.util.ArrayList;
import java.util.List;

import android.app.ActionBar;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

public class ListActivityH extends android.support.v4.app.FragmentActivity implements IHandlerCommunication {
    private List<ReadMessagePill> listMessageLoaded = new ArrayList<ReadMessagePill>();
    private long timeLastLoad = 0;
    private long timeLastThumbnail = 1;
    private boolean isLayoutInitiated = false;
    public View mapView = null;
    public GoogleMap mapGoogle = null;
    public LocationManager locationManager = null;
    public ProviderGpsListener providerGpsListener = null;
    public StatusMessageView myStatusView = null;
    public ReadMessageView hideReadMessage = null;
    public LinearLayout scrollViewH = null;
    public ViewGroup listHPanel = null;
    public ViewGroup listHBody = null;

    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.list_h);
	ActionBar ab = getActionBar();
	ab.hide();
	mapView = (View) findViewById(R.id.map);
	mapGoogle = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
	if (mapGoogle == null) {
	    Toast.makeText(this, "Google Play services not present. Impossible going on. App will be closed.", Toast.LENGTH_LONG).show();
	    finish();
	    return;
	}

	locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
	// providerGpsListener = new ProviderGpsListener(mapGoogle, Kernel.R().myStatusView);
	scrollViewH = (LinearLayout) findViewById(R.id.readMessageList);
	hideReadMessage = (ReadMessageView) findViewById(R.id.readMessageView);
	listHBody = (ViewGroup) findViewById(R.id.listHBody);
	listHPanel = (ViewGroup) findViewById(R.id.listHPanel);

	listHPanel.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
	    public void onGlobalLayout() {
		isLayoutInitiated = true;
		thumbnail();
	    }
	});

	load();
    }

    protected void onResume() {
	/*
	 * add location listener and request updates every 1000ms or 10m
	 */
	// locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 10f, providerGpsListener);
	super.onResume();
    }

    protected void onPause() {
	/* GPS, as it turns out, consumes battery like crazy */
	// locationManager.removeUpdates(providerGpsListener);
	super.onPause();
    }

    private void load() {
	timeLastLoad = System.currentTimeMillis();
	synchronized (listMessageLoaded) {
	    for (int yu = 0; yu < 15; yu++) {
		LinePill l1 = new LinePill(10, 10, 200, 200);
		LinePill l2 = new LinePill(20, 20, 80, 80);
		LinePill l3 = new LinePill(30, 40, 300, 700);
		ArrayList<LinePill> arrLine = new ArrayList<LinePill>();
		arrLine.add(l1);
		arrLine.add(l2);
		arrLine.add(l3);

		WordPill w1 = new WordPill("ciao1", 10, 20);
		WordPill w2 = new WordPill("ciao2", 10, 40);
		WordPill w3 = new WordPill("ciao3", 10, 80);
		ArrayList<WordPill> arrWord = new ArrayList<WordPill>();
		arrWord.add(w1);
		arrWord.add(w2);
		arrWord.add(w3);

		StatusPill status = new StatusPill("Descrizione: " + yu);

		ReadMessagePill msg = new ReadMessagePill(arrWord, arrLine, status);
		listMessageLoaded.add(msg);
	    }
	}
    }

    private void thumbnail() {
	if (!isLayoutInitiated)
	    return;

	if (timeLastLoad == timeLastThumbnail)
	    return;

	ReadMessagePill[] valuesClone = null;
	synchronized (listMessageLoaded) {
	    Object[] aux = listMessageLoaded.toArray();
	    valuesClone = new ReadMessagePill[aux.length];
	    for (int r = 0; r < aux.length; r++)
		valuesClone[r] = (ReadMessagePill) aux[r];
	}
	LayoutInflater inflater = this.getLayoutInflater();
	for (int u = 0; u < valuesClone.length; u++) {
	    ReadMessagePill rmp = valuesClone[u];
	    rmp.itemView = inflater.inflate(R.layout.item_h, null);
	    rmp.messageImageThumb = (ImageView) rmp.itemView.findViewById(R.id.readMessageImage);
	    rmp.statusView = (StatusMessageView) rmp.itemView.findViewById(R.id.statusMessageView);
	    scrollViewH.addView(rmp.itemView);
	    hideReadMessage.refresh(rmp.words, rmp.lines);
	    rmp.statusView.refresh(rmp.status);
	    int w = hideReadMessage.getWidth();
	    int h = hideReadMessage.getHeight();
	    int hT = scrollViewH.getHeight();
	    float scale = (float) hT / h;
	    Bitmap image = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_4444);
	    hideReadMessage.draw(new Canvas(image));
	    Matrix matrix = new Matrix();
	    matrix.postScale(scale, scale);
	    Bitmap resizedBitmap = Bitmap.createBitmap(image, 0, 0, w, h, matrix, true);
	    rmp.messageImageThumb.setImageBitmap(resizedBitmap);
	    rmp.itemView.setOnClickListener(new ClickThumbnailListener(this, rmp));
	}
	timeLastThumbnail = timeLastLoad;
	hideReadMessage.refresh(new ArrayList<WordPill>(), new ArrayList<LinePill>());
    }

    public void actionCreate(View v) {
	Intent intent = new Intent(this, DrawActivity.class);
	startActivity(intent);
    }

    public void actioStore(View v) {
	Intent intent = new Intent(this, ListActivityV.class);
	startActivity(intent);
    }

    public Handler getMyHandler() {

	return null;
    }

    @Override
    public void addExternalHandler(Handler handler) {
	// TODO Auto-generated method stub

    }
}